import cacheUtil from '@/utils/cache'

const hasPermission = (Vue) => {
  Vue.directive('has', {
    inserted: function (el, binding) {
      const isAuth = filterGlobalPermission(binding.value)
      if (!isAuth) {
        if (el.parentNode) {
          el.parentNode.removeChild(el)
        }
      }
    }
  })
}

export const filterGlobalPermission = (value) => {
  const authList = JSON.parse(
    cacheUtil.getItem('btnAuth') || []
  )
  if (!authList.length) {
    return false
  }
  if (!authList.includes(value)) {
    return false
  } else {
    return true
  }
}

export default hasPermission
